// exports.foo = (done) => {
//   console.log(123)
//   done();
// }

const fs = require('fs');
const { Transform } = require('stream');
const {
  series,
  parallel,
  src,
  dest
} = require('gulp');
const sass = require('gulp-sass');
const gulpCleanCss = require('gulp-clean-css');


const task1 = done => {
  console.log('task1');
  done();
}


const task2 = done => {
  console.log('task2');
  done();
}


const task3 = done => {
  console.log('task3');
  done();
}


// exports._series = series(task1, task2, task3);
// exports._parallel = parallel(task1, task2, task3);


exports.promise = () => {
  console.log('promise');
  return Promise.resolve();
}

exports.promise_err = () => {
  console.log('promise');
  return Promise.reject(new Error('错误'));
}

const promise = time => {
  return new Promise((resolve) => {
    setTimeout(resolve, time)
  })
}
//   setInterval(() => {
//     return Promise.resolve();
//   }, time)
// }

exports.async = async () => {
  await promise(1000);
  return false;
}


exports.stream = () => {

  const readStream = fs.createReadStream('css/reset.css');
  const writeStream = fs.createWriteStream('css/reset.min.css');
  const transform = new Transform({
    transform: function(chunk, encode, callback){
      const input = chunk.toString();
      const output = input.replace(/\s+/g, '').replace(/\/\*.+?\*\// , '');
      callback(null, output)
    }
  })
  readStream.pipe(transform).pipe(writeStream);
  return readStream;

}

const style = () => {
  return src('src/scss/*.scss', {base: 'src'})
    .pipe(sass())
    .pipe(gulpCleanCss())
    .pipe(dest('dist'));
}

module.exports = {
  style
}